//
//  TimetableController.swift
//  AirTravel
//
//  Created by apple on 2021/8/13.
//

import UIKit
import Moya
import SwiftyJSON

class TimetableController: UIViewController {

    @IBOutlet weak var tabView: UITableView!
    let identifyT = "TimetableCell"
    /// 用来主动取消网络请求
    var cancelableRequest: Cancellable?
    var dataSouce : Array<Any>?
    var dataStr = ""
    /// 车次
    var trainNo = ""
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.tintColor = UIColor.black
        self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.black]
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.white]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "列车时刻表"
        
        self.tabView.dataSource = self
        self.tabView.delegate = self
        self.tabView.rowHeight = 50
        self.tabView.tableFooterView = UIView.init()
        self.tabView.register(UINib.init(nibName: "TimetableCell", bundle: nil), forCellReuseIdentifier: identifyT)
        getStopStations()
    }
    
    //获取车次经停站接口
    func getStopStations() {
        cancelableRequest = NetWorkRequest(API1.getStopStations(parameters: ["departDate":self.dataStr,"trainNo":self.trainNo]), completion: {[weak self] (responseString) -> (Void) in
            let list = responseString["data"].arrayObject
            if let array = list {
                self?.dataSouce = array
            }
            self?.tabView.reloadData()
        }, failed: { (failedResutl) -> (Void) in
            print("服务器返回code不为0000啦~\(failedResutl)")
        }, errorResult: { () -> (Void) in
            print("网络异常")
        })
    }
}


extension TimetableController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSouce?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifyT) as! TimetableCell
        cell.selectionStyle = .none
        if let array = self.dataSouce {
            let dic = array[indexPath.row] as? Dictionary<String,Any>
            if indexPath.row == 0 || indexPath.row == array.count - 1 {
                cell.stationNameBtn.isSelected = true
            }else{
                cell.stationNameBtn.isSelected = false
            }
            cell.stationNameBtn.setTitle("\(dic?["stationName"] ?? "")", for: .normal)
            cell.startTimeLab.text = "\(dic?["arrivalTime"] ?? "")"
            cell.stopMinutesLab.text = "\(dic?["stopMinutes"] ?? "")"
            cell.arrivalTimeLab.text = "\(dic?["startTime"] ?? "")"
        }
        return cell
    }
    
    
}
